﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Linq;
using System.Linq;
using QLTVNEW.Properties;
using System.Windows.Forms;

namespace QLTVNEW
{
    public partial class frmPhieuMuon : DevComponents.DotNetBar.Office2007Form
    {
        public frmPhieuMuon()
        {
            InitializeComponent();
        }

        #region
        void txtReadOnly(bool a)
        {
            txtMaSach.ReadOnly=a;
            txtTen.ReadOnly=a;
        }

        void LoadDau()
        {
            cbxTenSach.Text = GVW.Rows[0].Cells[0].Value.ToString().Trim();
            txtTheLoai.Text = GVW.Rows[0].Cells[1].Value.ToString().Trim();
            txtMaSach.Text = GVW.Rows[0].Cells[2].Value.ToString().Trim();
            cbxMaDG.Text = GVW.Rows[0].Cells[3].Value.ToString().Trim();
            txtTen.Text = GVW.Rows[0].Cells[4].Value.ToString().Trim();
            mktMuonSach.Text = GVW.Rows[0].Cells[5].Value.ToString().Trim();
            mktTraSach.Text = GVW.Rows[0].Cells[6].Value.ToString().Trim();
            txtSoLuong.Text = GVW.Rows[0].Cells[7].Value.ToString().Trim();
        }

        void LoadGridView(DataGridView gv)
        {
                var query = from tphieu in db.PHIEUMUONTRASACHes
                            join tbdg in db.DOCGIAs on tphieu.MaDocGia equals tbdg.MaDocGia
                            join tbsach in db.SACHes on tphieu.MaSach equals tbsach.MaSach
                            where tphieu.LoaiMuonTra == "Mượn"
                            select new
                            {
                                tbsach.TenSach,
                                tbsach.THELOAI.TenTheLoai,
                                tbsach.MaSach,
                                tbdg.MaDocGia,
                                tbdg.HoTen,
                                tphieu.NgayMuonSach,
                                tphieu.NgayTraSach,
                                tbsach.SoLuong,

                            };
                gv.DataSource = query;
        }

        #endregion
        QLTVDataContext db = new QLTVDataContext();
        DateTime date = DateTime.Now;
        SACH tbsach = new SACH();
        DOCGIA tbdg = new DOCGIA();
        THELOAI tbtl = new THELOAI();
        PHIEUMUONTRASACH tbmuon = new PHIEUMUONTRASACH();
        private void frmPhieuMuon_Load(object sender, EventArgs e)
        {
      
            LoadGridView(GVW);
            tbsach.LoadComboBox(cbxTenSach);
            tbdg.LoadCBMuon(cbxMaDG);
            LoadDau();

        }

        private void btnclear_Click(object sender, EventArgs e)
        {
            foreach (Control ctr in groupBox1.Controls)
            {
                if (ctr is TextBox || ctr is ComboBox)
                {
                    ctr.Text = "";
                    labelX1.Focus();
                    mktMuonSach.Text = date.ToString("dd/MM/yyyy");
                    mktTraSach.Text = date.AddDays(14).ToString("dd/MM/yyyy");
                    
                }
                LoadGridView(GVW);
            }
        }

        private void cbxTenSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbxMaDG.Focus();
            txtMaSach.Text = cbxTenSach.SelectedValue.ToString();
            txtTheLoai.Text = tbsach.GetTheLoaiTheoMa(int.Parse(txtMaSach.Text));
            txtSoLuong.Text=tbsach.GetSoLuong(int.Parse(txtMaSach.Text));

        }

        private void cbxMaDG_SelectedIndexChanged(object sender, EventArgs e)
        {
            txtTen.Text = tbdg.GetTenDocGia(int.Parse(cbxMaDG.Text));
            labelX1.Focus();
        }
        
        private void btninsert_Click(object sender, EventArgs e)
        {
            try
            {
                string query = (from tbphieu in db.PHIEUMUONTRASACHes
                                where tbphieu.MaDocGia == int.Parse(cbxMaDG.Text) && tbphieu.LoaiMuonTra == "Mượn"
                                select tbphieu.NgayMuonSach).FirstOrDefault<System.String>();
                if (query==date.ToString("dd/MM/yyyy") || query==null)
                {
                    int dem = (from tbp in db.PHIEUMUONTRASACHes
                               where tbp.LoaiMuonTra == "Mượn" && tbp.MaDocGia == int.Parse(cbxMaDG.Text)
                               select tbp.LoaiMuonTra).Count();

                    if (dem >= 5)
                    {
                        MessageBox.Show("Bạn Chỉ Được Mượn Tối Đa 5 Cuốn", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                    else
                    {
                        if (int.Parse(txtSoLuong.Text) > 0)
                        {

                            if (cbxTenSach.Text != "" && cbxMaDG.Text != "")
                            {
                                try
                                {
                                    PHIEUMUONTRASACH q2 = db.PHIEUMUONTRASACHes.Where(p => p.MaDocGia == int.Parse(cbxMaDG.Text) && p.MaSach == int.Parse(txtMaSach.Text) && p.NgayMuonSach == mktMuonSach.Text).SingleOrDefault<PHIEUMUONTRASACH>();
                                    if (q2==null)
                                    {
                                        string err = "";
                                        db.uspCRUDPHIEUMUONTRA('t', int.Parse(cbxMaDG.Text), int.Parse(txtMaSach.Text), date.ToString("dd/MM/yyyy"), date.AddDays(14).ToString("dd/MM/yyyy"), 0, "Mượn", ref err);
                                        MessageBox.Show(err.ToString(), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                        db.SubmitChanges();
                                        LoadGridView(GVW);
                                    }
                                    else
                                        MessageBox.Show("Cuốn Sách Này Bạn Đã Mượn","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                                   
                                }
                                catch (System.Exception ex)
                                {
                                    MessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                }

                            }
                            else
                            {
                                MessageBox.Show("Phải Nhập Đầy Đủ Thông Tin", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                            }
                        }
                        else
                        {
                            MessageBox.Show("Số Lượng Sách Đã Hết.", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        }
                    }
                }
                else
                    MessageBox.Show("Xin Vui Lòng Trả Sách Trước Khi Bạn Muốn Mượn Tiếp", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
        int dong;
        private void GVW_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                dong = e.RowIndex;
                cbxTenSach.Text = GVW.Rows[dong].Cells[0].Value.ToString().Trim();
                txtTheLoai.Text = GVW.Rows[dong].Cells[1].Value.ToString().Trim();
                txtMaSach.Text = GVW.Rows[dong].Cells[2].Value.ToString().Trim();
                cbxMaDG.Text = GVW.Rows[dong].Cells[3].Value.ToString().Trim();
                txtTen.Text = GVW.Rows[dong].Cells[4].Value.ToString().Trim();
                mktMuonSach.Text = GVW.Rows[dong].Cells[5].Value.ToString().Trim();
                mktTraSach.Text = GVW.Rows[dong].Cells[6].Value.ToString().Trim();
                txtSoLuong.Text = GVW.Rows[dong].Cells[7].Value.ToString().Trim();
            }
            catch
            {
                return;
            }
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Muốn Xóa Không.","Thông Báo",MessageBoxButtons.YesNo,MessageBoxIcon.Question)==DialogResult.Yes)
            {
                if (cbxTenSach.Text!="" && cbxMaDG.Text!="")
                {
                        try
                        {
                            string err = "";
                            
                            db.uspCRUDPHIEUMUONTRA('x', int.Parse(cbxMaDG.Text), int.Parse(txtMaSach.Text), mktMuonSach.Text,mktTraSach.Text, 0, "", ref err);
                            MessageBox.Show(err.ToString(), "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                            db.SubmitChanges();
                            LoadGridView(GVW);
                        }
                        catch (System.Exception ex)
                        {
                            MessageBox.Show(ex.Message,"Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                        }
                 }
                 else
                        MessageBox.Show("Phải Nhập Đầy Đủ Thông Tin.","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Warning);
                }
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            var mangin = new int[5];
            try
            {
                int[] ma = (from tbp in db.PHIEUMUONTRASACHes
                          where tbp.NgayMuonSach == mktMuonSach.Text && tbp.LoaiMuonTra == "Mượn" && tbp.MaDocGia==int.Parse(cbxMaDG.Text)
                          select tbp.MaSach).ToArray();
                
                int i=0;
            
                string[] s=new string[5];
                string[] tl = new string[5];
                foreach (int  item in ma)
                {
                    // Lấy Tên Sách.
                    s[i] = (from tbSS in db.SACHes
                            where tbSS.MaSach == item
                            select tbSS.TenSach).SingleOrDefault<System.String>();
                    // Lấy Thể Loại.
                    tl[i] = (from tbss in db.SACHes
                             where tbss.MaSach == item
                             select tbss.THELOAI.TenTheLoai).SingleOrDefault<System.String>();
                    i++;
                }

               
                System.Text.StringBuilder sb = new System.Text.StringBuilder();
                
                for (int j = 0; j < s.Length;j++ )
                {
                    if (s[j]!=null)
                    {
                        sb.Append((j+1).ToString()+":"+ s[j] +"\n");
                       
                    }
                }
                cbxTenSach.Text = sb.ToString();
                sb.Length = 0;
                for (int j = 0; j < ma.Length;j++ )
                {
                   sb.Append((j + 1).ToString() + ":" + ma[j].ToString() + "\n");
                }
                txtMaSach.Text = sb.ToString();
                sb.Length = 0;
                for (int j = 0; j < tl.Length;j++ )
                {
                    if(tl[j]!=null)
                    {
                        sb.Append((j + 1).ToString() + ":" + tl[j].ToString() + Environment.NewLine);
                    }
                }
                txtTheLoai.Text = sb.ToString();


                frmInPhieuMuon frm = new frmInPhieuMuon();
                frm.tenDG = txtTen.Text;
                frm.maDG = cbxMaDG.Text;
                frm.tenSach = cbxTenSach.Text;
                frm.theLoai = txtTheLoai.Text;
                frm.maSach = txtMaSach.Text;
                frm.ngayMuon = mktMuonSach.Text;
                frm.ngayTra = mktTraSach.Text;
                frm.ShowDialog();
            }
            catch (System.Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void btncancel_Click(object sender, EventArgs e)
        {
            this.AutoValidate = AutoValidate.Disable;
            this.Close();
        }

        private void btnTra_Click(object sender, EventArgs e)
        {
            frmTraSach frm = new frmTraSach();
            frm.ShowDialog();
        }
    }
}
